package com.wjc.socket5;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import com.wjc.socket5webkit.Socket5WebkitActivity;

import android.net.http.*;
import android.util.Log;
import socks.*;

public class Socket5ConnectTcp {
		private int port;
	   private InetAddress hostIP;

	   private Socket ss;
	   private InputStream in;
	   private OutputStream out;

	   private static final int BUF_SIZE = 1024;
	   static final int defaultProxyPort = 1080;           //Default Port
	   static final String defaultProxyHost = "192.168.2.104"; //Default proxy
	public void Socket5ConectTcp(){
		
		
	
	}
	public String test(){
		Log.d("ddd", "ddd");
		Socks5Proxy p;
		try {
			
			p = new Socks5Proxy(null, "192.168.137.1", 1080);
			UserPasswordAuthentication k=new UserPasswordAuthentication("first","first");
			p.setAuthenticationMethod(UserPasswordAuthentication.METHOD_ID, k);
			SocksSocket s=new SocksSocket(p,"mail.gmail.com",443);
		
			s.bind(null);
			s.setKeepAlive(true);
			in=s.getInputStream();
			PrintWriter out = new PrintWriter(s.getOutputStream(), true); 
			 BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
	            //send an HTTP request to the web server
	           out.println("GET / HTTP/1.1"); 
	           out.println("Host: baidu.com"); 
	           out.println("Connection: Close"); 
	           out.println(); 
			
			byte data[]=new byte[1024];
			int length;
			String toreturn="";
			boolean loop=true;
			while (loop) { 
	              if (in.ready()) {
	                int i = 0;
	                String buffer;
	                	while ((buffer=in.readLine())!=null){
	                		toreturn+=buffer;
	                	}
	                loop = false; 
					}
	              
	            } 

			
				
			return toreturn;
		
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			Log.e("socket5Test", "unknownHost"+e.getMessage());
			return null;
		} catch (SocksException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("socket5Test", "socksError"+e.getMessage());
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			// TODO Auto-generated catch block
			Log.e("socket5Test", "io error"+e.getMessage());
			return null;
		}
	
	}

}
